package com.yft.dao.purchasepos;

import com.yft.dao.base.BaseConstant;
import com.yft.dao.base.BaseDao;
import com.yft.entity.base.BaseEntityUtils;
import com.yft.entity.base.Order;
import com.yft.entity.base.Pager;
import com.yft.entity.purchasepos.PosPurchaseOrderHeader;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;


@Service("posPurchaseOrderHeaderDao")
public class PosPurchaseOrderHeaderDao {
	
	@Autowired(required = true)
	@Qualifier(BaseConstant.DAO_NAME)
	private BaseDao dao;
    
    private int autoPurchaseHeaderCode = 1;

    /**
     * 生成4位流水号
     * @return id
     */
    @SuppressWarnings("unchecked")
    public synchronized String makePurchaseHeaderSerialNum(){
        String id = "";
        if(autoPurchaseHeaderCode==1){
            List<String> list = selectPropertis("select max(purchaseOrderNo) from PosPurchaseOrderHeader", null);
            if(list!=null && list.size()>0){
                String maxCode = list.get(0);
                if(maxCode!=null)
                    autoPurchaseHeaderCode = NumberUtils.toInt(maxCode.substring(11), 1);
            }
        }
        if(autoPurchaseHeaderCode==9999){
            autoPurchaseHeaderCode=1;
        }
        autoPurchaseHeaderCode ++;
        int newId = autoPurchaseHeaderCode;
        for (int i=String.valueOf(newId).length();i<4;i++){
            id += "0";
        }
        return id+newId;
    }

    @SuppressWarnings("unchecked")
    public List selectPropertis(String queryString,Object[] values) {
        if(values==null)
            return dao.getHibernateTemplate().find(queryString);
        else
            return dao.getHibernateTemplate().find(queryString, values);
    }

	public Object addPosPurchaseOrderHeader(PosPurchaseOrderHeader posPurchaseOrderHeader) {
		return dao.addEntity(posPurchaseOrderHeader);
	}
	
	public PosPurchaseOrderHeader getPosPurchaseOrderHeader(Object posPurchaseOrderHeaderPK){
		return (PosPurchaseOrderHeader)dao.getEntity(PosPurchaseOrderHeader.class,(Serializable)posPurchaseOrderHeaderPK);
	}
	

	public boolean updateValues(PosPurchaseOrderHeader condition,String customStr){
		return dao.updateValues(condition, customStr);
	}
	
	@SuppressWarnings("unchecked")
	public List<PosPurchaseOrderHeader> getPosPurchaseOrderHeaderList(PosPurchaseOrderHeader condition, Order order,Pager pager){
		return getList("",condition,order,pager);
	}	
	
	@SuppressWarnings("unchecked")
	private List getList(String hql,PosPurchaseOrderHeader condition,Order order,Pager pager){
		
		String q = hql+"from PosPurchaseOrderHeader c  where 1=1 ";
		q += makeCondition(condition);
		q += order!=null? order.toString():"";
		
		return dao.getListPager(q,condition,pager);
		
	}
	
	public String makeCondition(PosPurchaseOrderHeader condition){
		condition = condition==null? new PosPurchaseOrderHeader():condition;
		
		String q = "";
		q += BaseEntityUtils.makeBaseCondition(condition);
		
		return q;
	}
}
